package com.renwei.yunlong.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.work.PhotoDisplayActivity;
import com.renwei.yunlong.bean.HistoryBean;
import com.renwei.yunlong.http.Api;
import com.renwei.yunlong.utils.DateTimeUtils;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.utils.UtilList;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 工单 历史 adapter
 * <p>
 * Created by Administrator on 2017/9/11.
 */

public class WorkHistoryPagerAdapter extends BaseRecyclerViewAdapter<HistoryBean.Rows> {

    private static final int TYPE_TOP = 0x0000;
    private static final int TYPE_NORMAL = 0x0001;
    private String from;

    public WorkHistoryPagerAdapter(Context context) {
        super(context);
    }

    public WorkHistoryPagerAdapter(Context context, String from) {
        super(context);
        this.from = from;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_spare_application_process, null);
        return new WorkHistoryPagerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder rholder, final int position) {
        WorkHistoryPagerViewHolder holder = (WorkHistoryPagerViewHolder) rholder;
        final HistoryBean.Rows rows = getItem(position);
        if (getItemViewType(position) == TYPE_TOP) {
            holder.ivView.setImageResource(R.mipmap.dot_cur);
        } else {
            holder.ivView.setImageResource(R.mipmap.dot);
        }

        if (("到达".equals(rows.getOperationName()) && "1".equals(rows.getIsLast()))
                || ("出发".equals(rows.getOperationName()) && "1".equals(rows.getIsLast()))) {
            holder.tvAutograph.setVisibility(View.VISIBLE);
            holder.tvAutograph.setText("查看轨迹");
            holder.tvAutograph.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickLitener != null) {
                        mOnItemClickLitener.onItemClick(view, position);
                    }
                }
            });
        } else if ("1".equals(rows.getIsSpot())) {
            holder.tvAutograph.setVisibility(View.VISIBLE);
            holder.tvAutograph.setText("查看工单");
            holder.tvAutograph.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickLitener != null) {
                        mOnItemClickLitener.onItemClick(view, position);
                    }
                }
            });
        } else {
            holder.tvAutograph.setVisibility(View.GONE);
        }
        holder.tvContent.setText(rows.getHistroyContent());
        holder.tvName.setText(rows.getName());
        holder.tvType.setText(rows.getOperationName());

        if (!TextUtils.isEmpty(rows.getCreateTime())) {
            holder.tvTime.setText(DateTimeUtils.getMMddHHmm(rows.getCreateTime().replace("T", " ")));
        }

        // 附件 添加
        String images = StringUtils.value(rows.getAttachmentPath());
        String instead = StringUtils.value(rows.getIsInstead());
        if (!"".equals(images)) {
            final ArrayList<String> strings = spliteImages(images);
            holder.llAutograph.removeAllViews();
            for (int i = 0; i < UtilList.getCount(strings); i++) {
                if ("1".equals(instead)) {
                    strings.set(i, Api.$().SERVICE_FILE_URL + strings.get(i));
                } else if ("0".equals(instead)) {
                    strings.set(i, Api.$().OWNER_FILE_URL + strings.get(i));
                }
                View view = inflater.inflate(R.layout.item_view_autograph, holder.llAutograph, false);
                TextView tvName = view.findViewById(R.id.tv_name);
                File file = new File(strings.get(i));
                tvName.setText(StringUtils.value(file.getName()));
                final int finalI = i;
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PhotoDisplayActivity.OpenActivity(mContext, strings, finalI);
                    }
                });
                holder.llAutograph.addView(view);
            }

        } else {
            holder.llAutograph.removeAllViews();
        }
    }

    private ArrayList<String> spliteImages(String image) {
        ArrayList<String> stringArrayList = new ArrayList<>();
        if (image.contains("~")) {
            String[] tempImages;
            tempImages = image.split("\\~");
            if (tempImages != null && tempImages.length > 0) {
                for (int i = 0; i < tempImages.length; i++) {
                    if (tempImages[i].contains("#")) {
                        stringArrayList.add(tempImages[i].split("#")[1]);
                    }
                }
            }
        } else {
            if (image.contains("#")) {
                String strs = image.split("#")[1];
                if (strs.contains(",")) { // 返回的数据 错误格式
                    String[] split = strs.split(",");
                    stringArrayList.addAll(Arrays.asList(split));
                } else {
                    stringArrayList.add(strs);
                }
            }
        }
        return stringArrayList;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP;
        }
        return TYPE_NORMAL;
    }

    class WorkHistoryPagerViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.text_view)
        TextView textView;
        @Bind(R.id.iv_view)
        ImageView ivView;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_type)
        TextView tvType;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.ll_autograph)
        LinearLayout llAutograph;
        @Bind(R.id.tv_autograph)
        TextView tvAutograph;
        @Bind(R.id.rl_next)
        CardView rlNext;

        public WorkHistoryPagerViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
